package com.sghd.fight.robot.core;

import com.sghd.common.socket.client.Client;
import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.robot.action.Action;
import com.sghd.fight.robot.action.ConnAction;
import com.sghd.fight.robot.action.ErrorCode;
import com.sghd.fight.robot.action.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author wch
 * @date 2018-07-06 12:15
 * @desc 机器人
 **/
public class Robot {
    private Logger logger = LoggerFactory.getLogger(Robot.class);
    /**id*/
    private long robotId;
    public String name;
    /***/
    private Action action;
    private Client client;
    private int state = ErrorCode.SUCCESS;
    private boolean complete = true;
    private boolean alive = false;
    private boolean close = false;


    private Controller controller;

    public static Robot valueOf(long robotId, String name, Client client, Controller controller) {
        Robot robot = new Robot();
        robot.robotId = robotId;
        robot.name = name;
        robot.client = client;
        robot.controller = controller;
        robot.action = new ConnAction();
        return robot;
    }

    public void action() {
        if (canrun()) {
            complete = false;
            Stage stage = action.stage();
            int delay = RandomUtils.nextInt(stage.getDelay());
            if (stage == Stage.SKILL) {
                delay = RandomUtils.betweenInt(7000, 10000, true);
            }
            controller.addWork(new Runnable() {
                @Override
                public void run() {
                    synchronized (Robot.this) {
                        action.action(Robot.this);
                    }
                }
            }, delay);
        }
    }

    private boolean canrun() {
        return state == ErrorCode.SUCCESS && complete;
    }

    public void setNext(Action nextAction) {
        this.action = nextAction;
        state = ErrorCode.SUCCESS;
        complete = true;
        controller.addWork(new Runnable() {
            public void run() {
                action();
            }
        }, 0);
    }

    public boolean isClose() {
        return close;
    }

    public void setClose(boolean close) {
        this.close = close;
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public long getRobotId() {
        return robotId;
    }

    public void setRobotId(long robotId) {
        this.robotId = robotId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public Controller getController() {
        return controller;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }
}
